package sessions;

import java.sql.Date;
import java.util.LinkedList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import entities.Feedback;
import entities.HelpRequest;
import entities.Status;
import entities.User;
import exceptions.FeedbackException;
import exceptions.HelpRequestException;

/**
 * Session Bean implementation class HelpManager
 */
@Stateless
public class HelpManager implements HelpManagerRemote, HelpManagerLocal {

	@PersistenceContext(unitName = "SWIMv2")
	private EntityManager em;

	/**
	 * Default constructor.
	 */
	public HelpManager() {
		// TODO Auto-generated constructor stub
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see sessions.HelpManagerRemote#request(entities.User, entities.User,
	 * java.lang.String)
	 */
	@Override
	public void request(User fromUser, User toUser, String message)
			throws HelpRequestException {
		String from = fromUser.getUsername();
		String to = toUser.getUsername();
		if (!from.equals(to)) {
			List<HelpRequest> result = new LinkedList<HelpRequest>();
			Query q = em
					.createQuery("FROM HelpRequest hr WHERE hr.fromUser=:fromUser AND hr.toUser=:toUser");
			q.setParameter("fromUser", fromUser);
			q.setParameter("toUser", toUser);
			List<?> res = q.getResultList();
			for (Object obj : res) {
				result.add((HelpRequest) obj);
			}
			// if (result.isEmpty()) {
			HelpRequest hr = new HelpRequest();
			hr.setFrom(fromUser);
			hr.setTo(toUser);
			hr.setMessage(message);
			hr.setDate(new Date(System.currentTimeMillis()));
			hr.setStatus(Status.PENDING);
			em.persist(hr);
			// em.flush();
			return;
			// }
		}
		throw new HelpRequestException();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see sessions.HelpManagerRemote#fetchIncoming(entities.User)
	 */
	@Override
	public List<HelpRequest> fetchIncoming(String username) {
		List<HelpRequest> result = new LinkedList<HelpRequest>();
		Query q = em
				.createQuery("FROM HelpRequest hr WHERE hr.toUser.username=:username");
		q.setParameter("username", username);
		List<?> res = q.getResultList();
		for (Object obj : res) {
			result.add((HelpRequest) obj);
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see sessions.HelpManagerRemote#fetchOutgoing(entities.User)
	 */
	@Override
	public List<HelpRequest> fetchOutgoing(String username) {
		List<HelpRequest> result = new LinkedList<HelpRequest>();
		Query q = em
				.createQuery("FROM HelpRequest hr WHERE hr.fromUser.username=:username");
		q.setParameter("username", username);
		List<?> res = q.getResultList();
		for (Object obj : res) {
			result.add((HelpRequest) obj);
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see sessions.HelpManagerRemote#fetch(int)
	 */
	public HelpRequest fetch(int id) {
		Query q = em.createQuery("FROM HelpRequest hr WHERE hr.id=:id");
		q.setParameter("id", id);
		HelpRequest hr = (HelpRequest) q.getSingleResult();
		return hr;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see sessions.HelpManagerRemote#accept(entities.HelpRequest)
	 */
	@Override
	public void accept(HelpRequest hr) {
		hr.accept();
		em.merge(hr);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see sessions.HelpManagerRemote#refuse(entities.HelpRequest)
	 */
	@Override
	public void refuse(HelpRequest hr) {
		hr.refuse();
		em.merge(hr);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see sessions.HelpManagerRemote#addFeedback(int, java.lang.String,
	 * entities.HelpRequest)
	 */
	@Override
	public void addFeedback(int evaluation, String comment, HelpRequest hr)
			throws FeedbackException {
		List<Feedback> result = new LinkedList<Feedback>();
		Query q = em.createQuery("FROM Feedback f WHERE f.hr.id=:hrid");
		q.setParameter("hrid", hr.getId());
		List<?> res = q.getResultList();
		for (Object obj : res) {
			result.add((Feedback) obj);
		}
		if (result.isEmpty()) {
			Feedback feedback = new Feedback();
			feedback.setEvaluation(evaluation);
			feedback.setComment(comment);
			feedback.setHr(hr);
			em.persist(feedback);
			// em.flush();
			return;
		} else {
			throw new FeedbackException();
		}
	}

	@Override
	public List<Feedback> fetchReceivedFeedback(String username) {
		List<Feedback> result = new LinkedList<Feedback>();
		Query q = em
				.createQuery("FROM Feedback f WHERE f.hr.id in (SELECT hr.id FROM HelpRequest hr WHERE hr.toUser.username=:username)");
		q.setParameter("username", username);
		List<?> res = q.getResultList();
		for (Object obj : res) {
			result.add((Feedback) obj);
		}
		return result;
	}

	@Override
	public List<HelpRequest> fetchRequestWithFeedback() {
		List<HelpRequest> result = new LinkedList<HelpRequest>();
		Query q = em
				.createQuery("SELECT f.hr FROM Feedback f");
		List<?> res = q.getResultList();
		for (Object obj : res) {
			result.add((HelpRequest) obj);
		}
		return result;
	}

}
